package com.ai_chat.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.PublicKey;


@Component
public class RSA256Util {

    @Value("${rsa.publicKeyFile}")
    private String pubFile;
    @Value("${rsa.privateKeyFile}")
    private String priFile;


    public PublicKey getPublicKey(){
        try {
            ObjectInputStream pubIn = new ObjectInputStream(new FileInputStream(pubFile));
            PublicKey pubKey = (PublicKey) pubIn.readObject();

            // 关闭流
            pubIn.close();

            return pubKey;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    public PrivateKey getPrivateKey(){
        try {
            ObjectInputStream priIn = new ObjectInputStream(new FileInputStream(priFile));
            PrivateKey priKey = (PrivateKey) priIn.readObject();

            // 关闭流
            priIn.close();

            return priKey;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
